package controllers.pc;

import models.Models.Email;
import models.System.SQL.SQL;
import models.System.Systems.password;
import models.SystemModels.*;
import models.send.email;
import net.sf.json.JSONObject;
import play.Logger;
import play.db.jpa.JPA;


import javax.persistence.Query;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018-06-15.
 */

public class jsons extends Logo {

    public static String codes = null;
    public static String Email= null;

    public static void Logo(String name,String pwd){
        JSONObject json = new JSONObject();
        StringBuffer sb = new StringBuffer();
        sb.append(SQL.SELECT);
        sb.append(SQL.USER);
        String sql= String.format(" where UserName ='%s'and UserPwd='%s'",name,pwd);
        sb.append(sql);
        Query query = JPA.em().createQuery(sb.toString());
        List<SystemUser> user =query.getResultList();

        if (user.size()>0){
           json.put("code",1);

        }else{
            json.put("code",-1);
        }
        renderJSON(json);
    }

    public static void register(){
        String name= params.get("name");
        String pwd = password.pwd(params.get("pwd"));
        JSONObject json = new JSONObject();
        StringBuffer sb = new StringBuffer();
        sb.append(SQL.SELECT);
        sb.append(SQL.USER);
        String sql= String.format(" where UserName ='%s'and UserPwd='%s'",name,pwd);
        sb.append(sql);
        Logger.info(password.ids());
        Query query = JPA.em().createQuery(sb.toString());
        List<SystemUser> user =query.getResultList();
        if (user.size()>0){
            json.put("code",-1);
            renderJSON(json);
        }else{
            SystemUser systemUser = new SystemUser();
            systemUser.UserName = name;
            systemUser.UserPwd = pwd;
            systemUser.save();
            json.put("code",1);
            renderJSON(json);
        }

    }

    public static void user_design_picture(){
        String name = session.get("name");
        List<SystemUser> systemUserList = SystemUser.find("UserName",name).fetch();
        StringBuffer sb= new StringBuffer();
        sb.append(String.format("%s=%s",SQL.USERFOLDERS,systemUserList.get(0).Id));
        List<folders> foldersList = folders.find(sb.toString()).fetch();
        String url =  foldersList.get(0).folderUrl;
        JSONObject json = new JSONObject();
        if (url!=null){
            json.put("url",url);
        }

        renderJSON(json);
    }

    public static void users_design(){
        Long id = Long.parseLong(params.get("id"));
        Logger.info(id+"");
        SystemDesign systemdesign = SystemDesign.findById(id);
        renderJSON(systemdesign);
    }

    public static void verification_Code(){
        String id= params.get("md5");
        String code = params.get("md51");
        String Email =params.get("Email");
        JSONObject json = new JSONObject();
        if (id.equals(password.GetMD5Code(code))){
            SimpleDateFormat sdf =   new SimpleDateFormat("HHmmssms");
            StringBuffer sb = new StringBuffer();
            sb.append(sdf.format(new Date()));
            String i = sb.toString();
            String Id = password.pwd(i);
            if (i.isEmpty()){
                json.put("code",0);
            }else {
                try {
                    String ids = String.format("%s%s",i,Id);
                    codes =ids;
                    Logger.info(ids);
                    models.Models.Email emails = new Email();
                    emails.account = Email;
                    jsons.Email = Email;
                    emails.msg =String.format("回执编码为http://192.168.0.104:9000/personals?id=%s%s",i,Id);
                    emails.title =String.format("测试邮箱");
                    email.MsgEmail(emails);
                    json.put("code",1);
                    json.put("id",i);
                } catch (Exception ex){
                    ex.printStackTrace();
                    json.put("code",-1);
                }
            }
            renderJSON(json);
        }
            json.put("code",-2);
             renderJSON(json);

    }

    public static void modify(){
        JSONObject json = new JSONObject();
        try
        {
            SimpleDateFormat sdf =   new SimpleDateFormat("HHmmssms");
            StringBuffer sb = new StringBuffer();
            sb.append(sdf.format(new Date()));
            String i = sb.toString();
            String Id = password.pwd(i);
            String name = session.get("admin123456");
            String id= params.get("md5");
            String code = params.get("md51");
            String Confirm_the_password =params.get("Confirm_the_password");
            String Old_cipher =params.get("Old_cipher");
            id = password.GetMD5Code(code);
            String pwd = password.pwd(Old_cipher);
            Confirm_the_password = password.pwd(Confirm_the_password);
            sb.append(" FROM systemuser");
            sb.append(String.format(" where UserName ='%s' and UserPwd ='%s'",name,pwd));
            Logger.info(sb.toString());
            List<SystemUser> systemUserList = SystemUser.find(sb.toString()).fetch();


            if (systemUserList.size()>0){
                SystemUser systemUser = SystemUser.findById(systemUserList.get(0).Id);
                systemUser.UserName = name;
                systemUser.UserPwd = Confirm_the_password;
                systemUser.save();
                List<SystemUserDetails> systemUserDetailses = SystemUserDetails.find("UserId",systemUser.Id).fetch();
                Email emails = new Email();
                emails.account = Email;
                jsons.Email = systemUserDetailses.get(0).Email;
                emails.msg =String.format("您已经更改密码，回执编号为http://192.168.0.104:9000/personals?id=%s%s,更改后密码为%s",i,Id,Confirm_the_password);
                emails.title =String.format("测试邮箱");
                email.MsgEmail(emails);
                json.put("code",1);
                renderJSON(json);
            }
        }catch (Exception ex){
            json.put("code",-1);
            renderJSON(json);
        }

    }

    /**
     * 绑定用信息，如果不存在那么就开始增加
     */
    public static void personals(){
        String ReceiptNumber = params.get("id");
        String code =codes;
        List<SystemUserDetails> systemUserDetails = new ArrayList<SystemUserDetails>();
        if (code.toLowerCase().equals(ReceiptNumber.toLowerCase()))
            codes =null;
        if (!ReceiptNumber.toLowerCase().isEmpty())
            session.put("code",ReceiptNumber);
            session.put("name","admin");
        List<SystemUser> systemUsers = SystemUser.find("UserName",session.get("name")).fetch();
        for (int i = 0;i<systemUsers.size();i++){
            systemUserDetails = SystemUserDetails.find("UserId",systemUsers.get(i).Id).fetch();
        }
        try {
            if (!systemUserDetails.isEmpty()){
                Long Id = systemUserDetails.get(0).Id;
                SystemUserDetails systemUserDetail = SystemUserDetails.findById(Id);
                systemUserDetail.Email = Email;
            }
            pc_index.personals();;
        }catch (Exception ex){
            try{
                SystemUserDetails systemUserDetail = new SystemUserDetails();
                systemUserDetail.Email = Email;
                systemUserDetail.UserId = systemUsers.get(0).Id;
                systemUserDetail.save();
                pc_index.personals();
            }catch (Exception e){
                pc_error.databaseEx = e;
            }
        }
    }
    public static void pass(){
        renderJSON(password.pwd("wwww12222"));
    }
}
